<template>
  <div class="deploy">
    <div class="cardTop">开始聊天吧</div>
    <el-card class="elCard">
      <div class="left">
        <ul class="leftUl">
          <li class="leftLi">
            <div class="circle">
              <img src="../../assets/image/圆圈.svg" alt="" />
            </div>
          </li>
          <li class="leftLi2">
            <div class="circle">
              <img src="../../assets/image/圆圈.svg" alt="" />
            </div>
          </li>
        </ul>
        <div class="robot">
          <img src="../../assets/image/机器人.png" alt="" />
          <img src="../../assets/image/信息2.png" alt="" />
        </div>
      </div>
      <div class="right">
        <el-scrollbar height="660px">
          <div class="chat">
            <div class="chat-messages">
              <div
                v-for="(item, index) in messages"
                :key="item.id"
                class="message"
              >
                <div class="content">{{ item.text }}</div>
                <div class="date">{{ formatTimestamp(item.timestamp) }}</div>
                <div class="delate" @click="delateMsg(index)">
                  <img src="../../assets/image/删除.png" alt="" />
                  <div class="text">删除</div>
                </div>
                <div class="copy" @click="copyMsg(index)">
                  <img src="../../assets/image/复制.png" alt="" />
                  <div class="text">复制</div>
                </div>
              </div>
            </div>
          </div>
        </el-scrollbar>
        <div class="chat-input">
          <el-input
            v-model="input"
            @keydown.enter.prevent="sendMessage"
            placeholder="试着输入点什么..."
            class="input"
            type="textarea"
            :rows="3"
            autosize
            :style="{ maxHeight: '100px', overflowY: 'auto' }"
          >
          </el-input>

          <div class="clear" @click="clearInput">
            <img src="../../assets/image/清空.png" alt="" />
            <div class="text">清空输入框</div>
          </div>

          <el-button
            class="button"
            type="primary"
            @click="sendMessage"
            :icon="Position"
            >发送</el-button
          >
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import {
  ElButton,
  ElScrollbar,
  ElInput,
  ElCard,
  ElMessageBox,
  ElMessage
} from 'element-plus'
import { Position } from '@element-plus/icons-vue'
import { ref, nextTick } from 'vue'
const messages = ref([])
const input = ref('')
const text = ref('')

const sendMessage = async () => {
  if (input.value.trim() !== '') {
    messages.value.push({
      id: Date.now(),
      text: input.value,
      timestamp: new Date()
    })

    input.value = ''

    await nextTick() // 等待DOM更新完成  非常重要

    const messageElements = document.getElementsByClassName('message')
    const lastMessageElement = messageElements[messageElements.length - 1]
    lastMessageElement.scrollIntoView({ behavior: 'smooth', block: 'end' })
  }
}

const clearInput = () => {
  input.value = ''
}

const formatTimestamp = timestamp => {
  const options = {
    year: 'numeric',
    month: 'numeric',
    day: 'numeric',
    hour: 'numeric',
    minute: 'numeric',
    second: 'numeric'
  }
  return new Intl.DateTimeFormat('default', options).format(timestamp)
}

const copyMsg = index => {
  const text = messages.value[index].text
  const textarea = document.createElement('textarea')
  textarea.value = text
  document.body.appendChild(textarea)
  textarea.select()
  document.execCommand('copy')
  document.body.removeChild(textarea)
  ElMessage({
    type: 'success',
    message: '复制成功'
  })
}

const delateMsg = index => {
  ElMessageBox.confirm(`是否删除此数据吗?`, '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      messages.value.splice(index, 1)
      ElMessage({
        type: 'success',
        message: '删除成功'
      })
    })
    .catch(() => {})
}
</script>

<style lang="less" scoped>
.deploy {
  padding: 20px 60px 0 60px;

  .cardTop {
    margin-bottom: 20px;
    color: #27264d;
    font-size: 32px;
    opacity: 0.85;
    display: flex;
  }
  .elCard {
    height: 800px;
    .left {
      width: 300px;
      margin-right: 70px;
      flex: 0;
      display: flex;
      .leftUl {
        img {
          width: 20px;
          height: 20px;
        }
        .leftLi {
          display: flex;
          .circle {
            position: relative;
          }
          .circle::after {
            content: '';
            position: absolute;
            top: 60%;
            left: 50%;
            transform: translate(-50%, 0);
            width: 2px;
            height: 740px;
            background-color: #75b7ff;
          }
        }
        .leftLi2 {
          display: flex;
          margin-top: 720px;
        }
      }
      .robot {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        img {
          width: 40px;
          height: 40px;
        }
      }
    }
    .right {
      flex: 1;
      padding: 0 30px;
      border: 2px solid #ccc;
      border-radius: 7px;
      position: relative;
      .chat {
        height: 660px;
        padding: 10px;
        display: flex;
        flex-direction: column;

        .chat-messages {
          flex: 1;
          margin-right: 200px;

          .message {
            display: flex;
            flex-direction: column;
            margin-top: 50px;
            position: relative;

            .content {
              height: 200px;
              background: #f4f6f8;
              display: flex;
              padding: 2px 0 0 5px;
              border: 1px solid #ccc;
              word-wrap: break-word;
            }
            .date {
              position: absolute;
              align-self: flex-start;
              top: -20px;
              color: #ccc;
            }
            .delate {
              position: absolute;
              align-self: flex-end;
              bottom: 0px;
              right: -60px;
              display: flex;
              align-items: center;
              img {
                width: 20px;
                height: 20px;
              }
              .text {
                color: #1296db;
                opacity: 0;
                padding-left: 5px;
              }
              :hover {
                cursor: pointer;
              }
              :hover .text {
                opacity: 1;
              }
            }

            .copy {
              position: absolute;
              align-self: flex-end;
              bottom: 30px;
              right: -60px;
              display: flex;
              align-items: center;
              img {
                width: 20px;
                height: 20px;
              }
              .text {
                color: #1296db;
                opacity: 0;
                padding-left: 5px;
              }
              :hover {
                cursor: pointer;
              }
              :hover .text {
                opacity: 1;
              }
            }
          }
        }
      }
      .chat-input {
        display: flex;
        align-items: center;
        z-index: 1000;
        position: absolute;
        bottom: 10px;
        width: 95%;
        .input {
          margin-right: 10px;
        }
        .clear {
          width: 20px;
          height: 50px;
          display: flex;
          margin-left: 5px;
          justify-content: center;
          align-items: center;
          img {
            width: 20px;
            height: 20px;
          }

          .text {
            font-size: 12px;
            color: #1296db;
            margin-left: 5px;
            opacity: 0;
          }
          :hover {
            cursor: pointer;
            width: 100px;
          }

          :hover .text {
            opacity: 1;
            margin-right: 15px;
          }
        }

        .button {
          padding: 15px;
        }
      }
    }
  }

  :deep(.el-card__body) {
    display: flex;
    width: 100%;
  }
  :deep(.el-input__inner) {
    white-space: pre-wrap;
    word-break: break-all;
    height: 30px;
  }
}
</style>
